package com.example.spring_principle.component;

import com.example.spring_principle.model.Dog;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description: Bean 的生命周期
 * User: Li_yizYa
 * Date: 2025/7/31
 * Time: 16:25
 */
@Component
public class BeanLifeComponent implements BeanNameAware {


    private Dog dog;

    public BeanLifeComponent() {
        System.out.println("执行构造函数...");
    }

    @Autowired
    public void setDog(Dog dog) {
        this.dog = dog;
        System.out.println("使用 setter 方法，完成属性装配...");
    }

    @PostConstruct
    public void init() {
        System.out.println("执行 init 方法...");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("执行setBeanName, name:" + name);
    }

    public void use() {
        System.out.println("执行 use 方法...");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("执行 destroy 方法...");
    }
}
